<?php

/**
 * created by zzy
 * date: 2018/1/26 14:07
 */

namespace App\Api\Service\Plate\Controllers;


use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Plate;
use App\Engine\Func;
use App\Engine\KmPrinter;
use Framework\BaseClass\Api\Controller;
use Illuminate\Support\Facades\Log;

class PrintController extends Controller
{
    public $userId        = 25;
    public function index()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        //$printData ['id'=>版id,'number'=>打印数量]
        $printData          = request('print');
        if( !$userInfo || !$printData)
        {
            xThrow( ERR_PARAMETER );
        }
        $printData          = json_decode(htmlspecialchars_decode($printData), true);
        Log::info($printData);
        //$printData          = [['id'=>67,'number'=>2]];
        $plateIdArr         = $printNumberArr = [];
        foreach ($printData as $key=>$val)
        {
            $valId              = $val['id'];
            $valNumber          = $val['number'];
            $plateIdArr[]       = $valId;
            $printNumberArr[$valId] = $valNumber;
        }
        $where              = ['id'=>['in',$plateIdArr]];
        $plateColltion      = Plate::getList($where);
        $plateList          = $plateColltion->toArray();
        $result             = [];
        if(!empty($plateList))
        {
            foreach ($plateList as $key=>$val)
            {
                $plateId        = $val['id'];
                if(isset($printNumberArr[$plateId]))
                {
                    $printNumber        = $printNumberArr[$plateId];
                    KmPrinter::text($val['plate_no']);
                    KmPrinter::text($val['plate_name']);
                    $qrcodeArr          = [
                        'id:'.$plateId,'title:'.$val['plate_name']
                    ];
                    $qrcodeStr          = implode(',',$qrcodeArr);
                    KmPrinter::qrcode($qrcodeStr);
                    $temp = [
                        'number'=>$printNumber,
                        'pinfo'=>KmPrinter::p()
                    ];
                    $result[]           = $temp;
                }
            }
        }
        return $result;
    }
}